# 运行鸿蒙一键构建部署脚本（多 HSP 包模式）- Windows PowerShell版本
# 使用前请确保 hdc 已连接设备，DevEco Studio 工具链已安装

param(
  [switch]$Fast,        # 快速模式：仅启动能力（不安装依赖、不构建、不传输、不安装）
  [switch]$SkipDeps,    # 跳过依赖安装（ohpm install）
  [switch]$SkipBuild,   # 跳过构建（hvigorw assembleApp）
  [switch]$JustLaunch   # 仅启动能力（与 -Fast 等价）
)

# ================== 可配置变量 ==================
# 项目包名(根据实际项目包名修改!!!!!) 需与 AppScope/app.json5 中的 bundleName 保持一致
$BUNDLE_NAME = "xyz.terminal02.myapplication"
# ================== 可配置变量 ==================

# ================== 固定的变量 ==================
# 临时目录名（使用随机字符串避免冲突）
$TMP_DIR = "hm_deploy_tmp_" + [System.Guid]::NewGuid().ToString("N").Substring(0, 16)
# 构建产物路径（unsigned 输出）
$ENTRY_HAP = "products\entry\build\default\outputs\default\entry-default-unsigned.hap"
$BASIC_HSP = "commons\basic\build\default\outputs\default\basic-default-unsigned.hsp"
$HOME_HSP = "features\home\build\default\outputs\default\home-default-unsigned.hsp"
$MENU_HSP = "features\menu\build\default\outputs\default\menu-default-unsigned.hsp"
$MINE_HSP = "features\mine\build\default\outputs\default\mine-default-unsigned.hsp"
# ================== 固定的变量 ==================

# 设置错误时停止执行
$ErrorActionPreference = "Stop"

try {
    if ($Fast -or $JustLaunch) {
      Write-Host "快速模式：仅启动能力，不进行安装/构建/传输/安装" -ForegroundColor Yellow
      hdc shell aa start -a EntryAbility -b "$BUNDLE_NAME" -m entry
      Write-Host "✅ 已快速启动应用能力" -ForegroundColor Green
      exit 0
    }

    if (-not $SkipDeps) {
      Write-Host "1. 安装依赖..." -ForegroundColor Green
      ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl true
    } else {
      Write-Host "1. 跳过依赖安装（--SkipDeps）" -ForegroundColor Yellow
    }

    if (-not $SkipBuild) {
      Write-Host "2. 构建项目..." -ForegroundColor Green
      hvigorw assembleApp
    } else {
      Write-Host "2. 跳过构建（--SkipBuild）" -ForegroundColor Yellow
    }

    Write-Host "3. 停止正在运行的应用..." -ForegroundColor Green
    try {
        hdc shell aa force-stop "$BUNDLE_NAME"
    } catch {
        Write-Host "应用未运行或停止失败，继续执行..." -ForegroundColor Yellow
    }

    Write-Host "4. 创建设备临时目录..." -ForegroundColor Green
    Write-Host "  执行命令: hdc shell mkdir -p data/local/tmp/$TMP_DIR" -ForegroundColor Gray
    hdc shell mkdir -p "data/local/tmp/$TMP_DIR"

    Write-Host "5. 传输 HAP/HSP 包到设备..." -ForegroundColor Green
    Write-Host "  - 传输 entry.hap..." -ForegroundColor Cyan
    hdc file send "$ENTRY_HAP" "data/local/tmp/$TMP_DIR/"

    if (Test-Path $BASIC_HSP) {
        Write-Host "  - 传输 basic.hsp..." -ForegroundColor Cyan
        hdc file send "$BASIC_HSP" "data/local/tmp/$TMP_DIR/"
    } else {
        Write-Host "  - 未找到 basic.hsp，若安装失败请检查依赖配置" -ForegroundColor Yellow
    }

    if (Test-Path $HOME_HSP) {
        Write-Host "  - 传输 home.hsp..." -ForegroundColor Cyan
        hdc file send "$HOME_HSP" "data/local/tmp/$TMP_DIR/"
    } else {
        Write-Host "  - 未找到 home.hsp，若安装失败请检查依赖配置" -ForegroundColor Yellow
    }

    if (Test-Path $MENU_HSP) {
        Write-Host "  - 传输 menu.hsp..." -ForegroundColor Cyan
        hdc file send "$MENU_HSP" "data/local/tmp/$TMP_DIR/"
    } else {
        Write-Host "  - 未找到 menu.hsp，若安装失败请检查依赖配置" -ForegroundColor Yellow
    }

    if (Test-Path $MINE_HSP) {
        Write-Host "  - 传输 mine.hsp..." -ForegroundColor Cyan
        hdc file send "$MINE_HSP" "data/local/tmp/$TMP_DIR/"
    } else {
        Write-Host "  - 未找到 mine.hsp，若安装失败请检查依赖配置" -ForegroundColor Yellow
    }

    Write-Host "6. 安装应用包..." -ForegroundColor Green
    hdc shell bm install -p "data/local/tmp/$TMP_DIR"

    Write-Host "7. 清理设备临时文件..." -ForegroundColor Green
    hdc shell rm -rf "data/local/tmp/$TMP_DIR"

    Write-Host "8. 启动应用..." -ForegroundColor Green
    hdc shell aa start -a EntryAbility -b "$BUNDLE_NAME" -m entry

    Write-Host "✅ 构建部署完成！应用已成功启动" -ForegroundColor Green

} catch {
    Write-Host "❌ 构建部署失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "请检查错误信息并重试" -ForegroundColor Red
    exit 1
}